AppleScript is an English-like programming language that allows you to use Apple Events to control applications.
Great. Whatユs an Apple Event?
With the advent of System 7, Apple introduced a powerful new way for applications to communicate with each other. Each Apple Event represents a single メmessageモ in this digital dialog.
For instance, when you double-clicked this document, the Finder actually sent an メopen documentモ Apple Event to Tex-Edit. Tex-Edit then recognized the event and displayed this document for you.
Whatユs the big deal?
Well, Apple Events allow メscriptableモ applications to communicate at a very intimate level. Unlike macros, Apple Events bypass the user interface and are quite efficient.
AppleScript puts the power of these Apple Events in the hands of the ordinary user. Itユs just another insanely great advantage that we Mac users enjoy.
What does it mean to say an application is メscriptable?モ
Scriptability implies that the programmer has given AppleScript access to major portions of the applicationユs inner workings.
If AppleScript is so great, why arenユt all applications scriptable?
Changing an existing application to make it scriptable involves an effort that I like to refer to as メnon-trivial.モ Luckily, most significant applications are now scriptable.
Do I have to learn to program to use AppleScript?
Well, it kind of depends on what you mean by メprogramming.モ The AppleScript dialect is very forgiving and looks a lot like ordinary English.
No you donユt understand. I REALLY hate programming!
Youユre in luck!
Tex-Edit, like many other scriptable applications, is also メrecordable.モ Appleユs Script Editor can record your actions as you use Tex-Edit. The resulting メscriptモ (program) can be saved and re-played later.
So, AppleScript isnユt just for nerds?
The real nerds are the folks who waste time doing repetitive computing chores, instead of letting their Mac do the work.
Okay, show me an example of an AppleScript program.
Well, letユs see. How about if we create a メstyle sheetモ program which sets the font of the text to 12 point Monaco (ick!) and then sets the size of the first character of every paragraph to 24 points.
Just how much programming work would that require?
You just saw it.
Huh?
Well, actually the program might look something like this:
tell application "Tex-Edit Plus"
set the font of the text of the first window to "Monaco"
set the size of the text of the first window to 12
set the size of the first character of every paragraph of window 1 to 24
end tell
Not bad! What does the first line do?
Itユs just a shortcut. Notice how the メtellモ and メend tellモ lines bracket all the commands. This saves us a little typing, otherwise the program would read:
set the font of the text of the first window
of application "Tex-Edit Plus" to "Monaco"
set the size of the text of the first window
of application "Tex-Edit Plus" to 12
set the size of the first character of every paragraph of window 1
of application "Tex-Edit Plus" to 24
But, doesnユt every word have to be in just the right place?
AppleScript is a very forgiving programming language. For example, these two programs do the same thing:
set the size of the first word to ten
set size of word 1 to 10
Okay already! How do I add scripts to Tex-Editユs □ menu?
When Script Editor saves a script as a メcompiled script,モ it creates a script document which can be placed in Tex-Editユs メScriptsモ folder. When Tex-Edit launches, it adds everything in this folder to its Scripts menu, providing easy access to your favorite scripts.
I donユt see a □ menu.
Either there is no メScriptsモ folder in the same folder as Tex-Edit or you have a System version earlier than 7.5.
Well, I have System 7.5, but I meticulously removed lots of stuff in the Extensions folder to make my system more stable.
Donユt do that! You probably managed to remove lots of optimized code patches and bug fixes. Itユs not worth the tiny amount of disk space you saved.
I want to install AppleScript. Where can I find it?
AppleScript and Script Editor are included free with all Systems since 7.5. If you have an earlier version of System 7, you can download the latest AppleScript installer from Appleユs web site:
<http://applescript.apple.com/>
How can I learn more about writing scripts?
AppleScript is a rich, full-featured programming language with variables, subroutines, loops and branches. If you want to appreciate the full power of AppleScript, I would recommend Danny Goodmanユs メAppleScript Handbook.モ Itユs an excellent source of information, especially for beginners.
You should also subscribe to the MACSCRPT mailing list. It is very active and includes lots of helpful information from lots of AppleScript users. To subscribe to MACSCRPT:
send email to: <mailto:LISTSERV@dartmouth.edu>
with subject: メsubscribeモ
and message: メsubscribe macscrpt <your full name here>モ.
Finally, just experiment with Script Editorユs メrecordモ function to see examples of the syntax Tex-Edit uses when sending messages to itself.
But, I want to write a script NOW!
Okay, first launch Tex-Edit and then launch Script Editor (which should be located in the Apple menu). To execute an AppleScript command, simply type the command into the Script Editor window, then press the メrunモ button:
□
In an AppleScript program, each line is a separate command. Usually the commands are bracketed by メtellモ and メend tellモ statements, as explained above. When the program runs, command lines are executed sequentially from top to bottom.
Try typing in and running the following program. (Lines which start with a double hyphen are optional comments.)
□
Once you get a program to do what you want it to do, save it as a メcompiled scriptモ document inside of Tex-Editユs メScriptsモ folder.
What commands does Tex-Edit understand?
Every scriptable application contains a メdictionaryモ which can be explored using Script Editor. I have included a brief synopsis of Tex-Editユs dictionary below, but I would recommend using Script Editor to print out the real thing.
How do I interpret whatユs in the dictionary?
The dictionary is divided up into chapters (or メsuitesモ)--Required Suite, Standard Suite, Text Suite, etc. Each suite heading is followed by a list of commands and objects (or メclassesモ).
The dictionary defines every AppleScript word that is recognized by Tex-Edit, but it does not explain all the different possible usages. Try using Script Editorユs メrecordモ button to see examples of acceptable syntax.
Whatユs an object?
In the program:
close window 1 of application "Tex-Edit Plus"
the verb メcloseモ is the command and メwindow 1 of application Tex-Edit Plusモ is the object.
Many of the objects have メproperties.モ You can use the Get and Set commands (described below) to extract an objectユs property and change it to something different. Notice how the メofモ keyword can chain together a list of objects, allowing you to unambiguously specify any given object.
tell application "Tex-Edit Plus"
get name of window 1
set size of character 1 of word 1 of window "untitled" to 24
end tell
In the first command, メnameモ is a property of the object メwindow 1 of application Tex-Edit Plus.モ In the second command, メsizeモ is a property of the object メcharacter 1 of word 1 of window "untitled" of application Tex-Edit Plus.モ
_____________________________
Commonly-used commands:
Here are some of the basic commands understood by most applications along with some simple examples of usage. Notice that many commands correspond to standard menu items. Examine each example to see the basic syntax. Of course, the best way to understand the commands is to actually type them in and try them out.
activate
Brings an application to the front.
ex:
activate application "Tex-Edit Plus"
quit
Quits an application.
ex:
quit application "Tex-Edit Plus"
quit application "Tex-Edit Plus" saving no
open
Opens a document file.
ex:
tell application "Tex-Edit Plus"
open file "MacHD:Saving the World"
end tell
print
Prints a document or window.
ex:
print every window of application "Tex-Edit Plus"
print the last window of application "Tex-Edit Plus"
close
Closes the given window.
ex:
close every window of application "Tex-Edit Plus"
close windows 2 through 4 of application "Tex-Edit Plus"
count
Returns the number of items.
ex:
count the words of window 2 of application "Tex-Edit Plus"
count the windows of application "Tex-Edit Plus"
delete
Deletes the given text.
ex:
tell window 1 of application "Tex-Edit Plus"
delete the first word
delete every character whose color is red
end tell
get
Gets whatever information you ask for. Get and Set are used a lot!
ex:
tell application "Tex-Edit Plus"
get the font of the third word of window 1
get the position of the first window
end tell
set
Changes the value of an objectユs property.
ex:
tell window 1 of application "Tex-Edit Plus"
set the color of the last word to red
set the name to "Fun With AppleScript"
end tell
make
Creates a new window or text item.
ex:
tell application "Tex-Edit Plus"
make new window behind window 1
make new line at end of text of window 1 with data "Dear Occupant:"
end tell
save
Saves the window to disk.
ex:
tell application "Tex-Edit Plus"
save the first window in "MacHD:Letter to MicroFlaccid"
end tell
select
Selects the desired text or window.
ex:
tell application "Tex-Edit Plus"
select the last document of application "Tex-Edit Plus"
select text from character 1 to character 60000 of window 2
end tell
undo/copy/cut/paste/revert
Executes the familiar menu command.
ex:
tell application "Tex-Edit Plus"
copy
copy unstyled
end tell
Other commands (specific to Tex-Edit):
add line endings
Adds CRs and LFs to the text.
ex:
tell selection of application "Tex-Edit Plus"
add line endings with crlf
end tell
block format
Converts the given text into an indented block.
ex:
tell the first window of application "Tex-Edit Plus"
block format the first paragraph with line header "> "
block format the last paragraph with line length 45
end tell
change case
Changes the case of the given text.
ex:
tell the middle window of application "Tex-Edit Plus"
change case of line 3 to uppercase
end tell
search
Searches for the given text.
ex:
search the last window of application "Tex-Edit Plus" for "important item"
replace
Replaces all occurrences of one string with another.
ex:
tell application "Tex-Edit Plus"
replace window 1 from "Copland" to "Allegro"
replace the last window from multiple spaces to tab
end tell
smarten/stupefy
Transforms to/from typographical characters.
ex:
smarten the first window of application "Tex-Edit Plus" for quotes
speak
Read the given text aloud.
ex:
speak "hello"
speak first paragraph of the third window of application "Tex-Edit Plus"
strip
Remove the specified characters from the text.
ex:
tell application "Tex-Edit Plus"
strip the first window for leading spaces
end tell
strip line endings
Remove commonly used line ending characters.
ex:
tell window 2 of application "Tex-Edit Plus"
strip line endings with cr
strip line endings of selection with crlf
end tell
Objects:
Many objects can be combined with the preceding commands into meaningful statements, however some combinations are not allowed.
For example you can:
close window 1 of application "Tex-Edit Plus"
print window 1 of application "Tex-Edit Plus"
but you canユt:
delete window 1 of application "Tex-Edit Plus"
quit window 1 of application "Tex-Edit Plus"
メTrial and errorモ is a good way to get started. It is also helpful to use Script Editorユs メrecordモ function to watch what happens as you use Tex-Edit. Note the use of the Get and Set commands to access the properties of each object. Here are a few of the available objects along with a few of their properties:
application
This is Tex-Edit itself. Its properties are メglobal.モ
properties:
selection: The current text selection in the front window.
search string: The string used in the Find dialog.
speech on: Used to turn on/off the Speech Manager functions.
ex:
tell application "Tex-Edit Plus"
get selection
set font of selection to "Monaco"
set search string to "interesting stuff"
set speech on to true
end tell
window
This is synonymous with メdocument.モ
properties:
name: The title of the document.
position: The coordinates of the top left corner.
justification: Used to set text justification in the window.
ex:
tell application "Tex-Edit Plus"
get name of window 1
set the position of some window to {125, 250}
set justification of every document to center
end tell
character
This is a single character in a window.
font: The font face of the character.
size: Font size of the character.
style: Styles of the character (bold, italic, etc.).
color: Color of the character.
ex:
tell window 3 of application "Tex-Edit Plus"
get the font of character 1
set size of (text from character 1 to word 10) to 24
set the style of character 1 to {bold, italic, underline}
set color of the last character to red
end tell
word-line-paragraph-text
These objects share all the properties of character.
ex:
tell window 1 of application "Tex-Edit Plus"
get the font of word 1
set the color of paragraph 2 to blue
set size of line 1 to 24
set style of (text from word 2 to word 4) to bold
end tell
_____________________________
AppleScript seems so simple. Are there any limits?
This document barely brushes the surface of the possibilities. My favorite trick is to use voice-actuated メstyle sheetモ scripts to switch styles as I type.
The sample scripts in Tex-Editユs メScriptsモ folder will help you get started. Feel free to examine them and change them as much as you wish.
Where can I find more sample scripts?
There are hundreds of useful scripts located all over the web, available to anyone with a modem and a phone jack. Also, be sure to check out the numerous AppleScript extensions (OSAX) that add even more power.
So, what are you waiting for? You have nothing to lose but your tired fingers.